<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/20
 * Time: 18:01
 */

namespace app\houtai\controller\user\glod;
use app\houtai\controller\BaseController;
use think\Request;

class GlodController extends BaseController
{

     public function indexAction(){
         $conds = [];
         $order = [];
         $CHANNELS   = $this->GetAdminChannel();
         $GAME_KIND  = GetGameKind();
         $Channel = $this->request->param('Channel/d', -1);
         if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
             $conds['c.Channel'][] = ['IN', array_keys($CHANNELS)];
             $conds['c.Channel'][] = ['EQ', $Channel];
         } else {
             $conds['c.Channel'] = ['IN', array_keys($CHANNELS)];
         }
         //////////////////////////////
         $KindID = $this->request->param('KindID/d', -1);
         if ($KindID > 0 && array_key_exists($KindID, $GAME_KIND)) {
             $conds['b.KindID'] = $KindID;
         }
         $time1 = $this->request->param('time1/s', '', 'trim');
         $time2 = $this->request->param('time2/s', '', 'trim');
         $aa = YmdHis2Unix($time1);
         $bb = YmdHis2Unix($time2);
         $ta = date('Y-m-d 00:00:00', $aa);
         $tb = date('Y-m-d 23:59:59', $bb);
         if ($aa > 0) {
             if ($bb > 0) {
                 if ($aa <= $bb) {
                     $conds['b.InsertTime'] = ['BETWEEN TIME', [$ta, $tb]];
                 } else {
                     $conds['b.InsertTime'] = ['>= TIME', $ta];
                 }
             } else {
                 $conds['b.InsertTime'] = ['>= TIME', $ta];
             }
         } else {
             if ($bb > 0) {
                 $conds['b.InsertTime'] = ['<= TIME', $tb];
             }
         }
         ////////////////////////////////////////////////////////////////////////////
         $UserID = $this->request->param('UserID/d', 0); // 用户号
         if ($UserID > 0) {
             $conds['a.UserID'] = ['eq',$UserID];
         }
         ////////////////////////////////////////////////////////////////////////////
         $orderMap = [
             'A' => 'DrawID',
             'C' => 'KindID',
             'D' => 'ServerID',
             'E'  => 'UserID',
             'F' => 'Score',
             'G' => 'Score',
             'H' => 'InsertTime',
         ];
         $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'A';
         $order[$orderMap[$this->orderField]] = $this->orderDirection;

         $list = table('RecordDrawScore a', 'rydb.record')->field([
             'b.KindID'=>'KindID',
             'a.UserID'=>'UserID',
             'a.DrawID'=>'DrawID',
             'a.ChairID',
             'a.Score'=>'Score',
             'b.ServerID'=>'ServerID',
             'f.ServerName',
             'b.StartTime'=>'TimeStart',
             'b.ConcludeTime'=>'TimeEnd','b.InsertTime', 'c.GameID', 'c.Accounts', 'c.NickName',
             'c.AccountsType', 'c.LastLogonIP', 'c.RegisterIP', 'c.GameLogonCount', 'c.LastLogonDate',
             'c.RegisterDate', 'c.RegisterMobile', 'c.Compellation', 'c.PassPortID', 'c.AccountImgUrl',
             'c.Sex', 'c.Channel', 'e.GameName'=>'KindName',
             ])
             ->join('RecordDrawInfo b','b.DrawID = a.DrawID','LEFT')
             ->join('CYBasicDB.dbo.AccountsInfo c','c.UserID = a.UserID','LEFT')
             ->join('CYBasicDB.dbo.GameKindItem e','e.KindID = b.KindID','LEFT')
             ->join('CYBasicDB.dbo.GameRoomInfo f','f.ServerID = b.ServerID','LEFT')
             ->where($conds)->order($order)->paginate();

         ////////////////////////////////////////////////////////////////////////////
         return $this->fetch('', [
             'list'  => $list,
             'UserID'=> empty($UserID) ? '' : $UserID,
             'CHANNELS' => $CHANNELS,
             'GAME_KIND' => $GAME_KIND,
             'Channel'   => $Channel,
             'KindID'    => $KindID,
             'time1' => $time1,
             'time2' => $time2,
         ]);
     }
    /**
    *   @brief  : 金币场详情信息
     *  @return : fetch
     *  @details:
     */
    public function detailAction(){
        $DrawID = $this->request->param('DrawID/d',0);
        $CHANNELS   = $this->GetAdminChannel();
        $gameinfo = table('RecordDrawInfo','rydb.record')
            ->where('DrawID',$DrawID)->value('DrawGameInfo');
        $jso = json_decode($gameinfo,true);
//        $gameinfo = table('ConfigServerIP','rydb.websms')
//            ->select();
//        $aa= json_encode($gameinfo);
//        var_dump($gameinfo);
//        echo $aa;die;

        if ($jso['KindID'] == 110){    //KindID=110/百人牛牛
            $jso['BankerCard'] = explode(',',$jso['BankerCard']);
            $jso['DCard'] = explode(',',$jso['DCard']);
            $jso['XCard'] = explode(',',$jso['XCard']);
            $jso['NCard'] = explode(',',$jso['NCard']);
            $jso['BCard'] = explode(',',$jso['BCard']);
            $jso['WinResult'] = explode(',',$jso['WinResult']);
            $jso['WinBeiShu'] = explode(',',$jso['WinBeiShu']);

    //庄家牌型
            if ($jso['BankerCard'][5]<3){
                $jso['BankerCard'][5] = 0;
            }else if($jso['BankerCard'][5] > 12){
                $jso['BankerCard'][5] = 10;
            }else{
                $jso['BankerCard'][5] = floor($jso['BankerCard'][5] -2);
            }
    //黑桃桌牌型
            if ($jso['DCard'][5]<3){
                $jso['DCard'][5] = 0;
            }else if($jso['DCard'][5] > 12){
                $jso['DCard'][5] = 10;
            }else{
                $jso['DCard'][5] = floor($jso['DCard'][5] -2);
            }
    //红桃桌牌型
            if ($jso['XCard'][5]<3){
                $jso['XCard'][5] = 0;
            }else if($jso['XCard'][5] > 12){
                $jso['XCard'][5] = 10;
            }else{
                $jso['XCard'][5] = floor($jso['XCard'][5] -2);
            }
    //梅花桌牌型
            if ($jso['NCard'][5]<3){
                $jso['NCard'][5] = 0;
            }else if($jso['NCard'][5] > 12){
                $jso['NCard'][5] = 10;
            }else{
                $jso['NCard'][5] = floor($jso['NCard'][5] -2);
            }
    //方片桌牌型
            if ($jso['BCard'][5]<3){
                $jso['BCard'][5] = 0;
            }else if($jso['BCard'][5] > 12){
                $jso['BCard'][5] = 10;
            }else{
                $jso['BCard'][5] = floor($jso['BCard'][5] -2);
            }
            $StartTime = table('RecordDrawInfo', 'rydb.record')->where('DrawID',$DrawID)->value('StartTime');
            $EndTime = table('RecordDrawInfo', 'rydb.record')->where('DrawID',$DrawID)->value('ConcludeTime');

            for($i=0;$i<count($jso['UserInfo']);$i++){
                    $info = table('CYBasicDB.dbo.AccountsInfo a', 'rydb.basic')->field([
                        'a.GameID','a.UserID',
                        'a.AccountsType', 'b.AccountImgUrl'=>'HeadImgUrl'
                    ])  ->join('CYBasicDB.dbo.RecordDrawScore b', 'b.UserID = a.UserID', 'LEFT')
                        ->where(['a.UserID' => $jso['UserInfo'][$i]['userid'], 'b.DrawID' => $DrawID])->find();
                    $jso['UserInfo'][$i]['HeadImgUrl'] = $info['HeadImgUrl'];
                    $jso['UserInfo'][$i]['GameID'] = $info['GameID'];
                    $jso['UserInfo'][$i]['UserID'] = $info['UserID'];
                    $jso['UserInfo'][$i]['AccountsType'] = $info['AccountsType'];
                    $jso['UserInfo'][$i]['addScore'] = explode(',', $jso['UserInfo'][$i]['addScore']);
            }

            //玩家每桌下注分数
            for($i=0;$i<count($jso['UserInfo']);$i++){
                for ($k=0;$k<count($jso['UserInfo'][$i]['addScore']);$k++) {
                    if($jso['WinResult'][$k] == 0) {
                        $jso['UserInfo'][$i]['addScore'][$k] = '-'.$jso['WinBeiShu'][$k] * $jso['UserInfo'][$i]['addScore'][$k] . '|下注:' . $jso['UserInfo'][$i]['addScore'][$k];
                    }else{
                        $jso['UserInfo'][$i]['addScore'][$k] = '+'.$jso['WinBeiShu'][$k] * $jso['UserInfo'][$i]['addScore'][$k] . '|下注:' . $jso['UserInfo'][$i]['addScore'][$k];
                    }
                }
            }

            return $this->fetch('hundredetail', [
                //'CHANNELS' => $CHANNELS,
                'BankerCard'         => $jso['BankerCard'],
                'DCard'              => $jso['DCard'],
                'XCard'              => $jso['XCard'],
                'NCard'              => $jso['NCard'],
                'BCard'              => $jso['BCard'],
                'u'                  => $jso['UserInfo'],
                'WinResult'          => $jso['WinResult'],
                'WinBeiShu'          => $jso['WinBeiShu'],
                'StartTime'         => $StartTime,
                'EndTime'           => $EndTime,
            ]);
        }else {

            //KindID=32，50，52||执行判断机器人数据
            $a = count($jso['UserInfo']);
            for ($i = 0; $i < $a; $i++) {
                $jso['UserInfo'][$i]['card'] = explode(',', $jso['UserInfo'][$i]['card']);
                if (in_array($jso['UserInfo'][$i]['cardType'], [242, 243, 244, 245, 246, 247, 248, 248, 250])) {
                    $jso['UserInfo'][$i]['cardType'] = $jso['UserInfo'][$i]['cardType'];
                } else {
                    $jso['UserInfo'][$i]['cardType'] = floor(($jso['UserInfo'][$i]['cardType'] - 1) / 20);
                }
                if ($jso['UserInfo'][$i]['userid'] < 1000000) {
                    $info = table('CYBasicDB.dbo.AccountsInfo a', 'rydb.basic')->field([
                        'c.KindID',
                        'c.DrawID',
                        'b.ChairID',
                        'b.Score',
                        'c.StartTime' => 'TimeStart',
                        'c.ConcludeTime' => 'TimeEnd', 'a.GameID',
                        'a.AccountsType', 'a.AccountImgUrl'=>'HeadImgUrl', 'a.Channel', 'e.KindName', 'f.ServerName'
                    ])  ->join('CYRecordDB.dbo.RecordDrawScore b', 'b.UserID = a.UserID', 'LEFT')
                        ->join('CYRecordDB.dbo.RecordDrawInfo c', 'c.DrawID = b.DrawID', 'LEFT')
                        ->join('CYBasicDB.dbo.GameKindItem e', 'e.KindID = c.KindID', 'LEFT')
                        ->join('CYBasicDB.dbo.GameRoomInfo f', 'f.ServerID = c.ServerID', 'LEFT')
                        ->where(['a.UserID' => $jso['UserInfo'][$i]['userid'], 'b.DrawID' => $DrawID])->find();

                    $jso['UserInfo'][$i]['DrawID'] = $info['DrawID'];
                    $jso['UserInfo'][$i]['ChairID'] = $info['ChairID'];
                    $jso['UserInfo'][$i]['Score'] = $info['Score'];
                    $jso['UserInfo'][$i]['TimeStart'] = $info['TimeStart'];
                    $jso['UserInfo'][$i]['TimeEnd'] = $info['TimeEnd'];
                    $jso['UserInfo'][$i]['HeadImgUrl'] = $info['HeadImgUrl'];
                    $jso['UserInfo'][$i]['Channel'] = $info['Channel'];
                    $jso['UserInfo'][$i]['KindID'] = $info['KindID'];
                    $jso['UserInfo'][$i]['KindName'] = $info['KindName'];
                    $jso['UserInfo'][$i]['GameID'] = $info['GameID'];
                    $jso['UserInfo'][$i]['ServerName'] = $info['ServerName'];
                    $jso['UserInfo'][$i]['AccountsType'] = $info['AccountsType'];
                } else {

                    $jso['UserInfo'][$i]['UserID'] = '1000001';
                    $jso['UserInfo'][$i]['DrawID'] = '--';
                    $jso['UserInfo'][$i]['ChairID'] = '机器人';
                    $jso['UserInfo'][$i]['Score'] = '--';
                    $jso['UserInfo'][$i]['TimeStart'] = '--';
                    $jso['UserInfo'][$i]['TimeEnd'] = '--';
                    $jso['UserInfo'][$i]['HeadImgUrl'] = '';
                    $jso['UserInfo'][$i]['Channel'] = '--';
                    $jso['UserInfo'][$i]['KindID'] = '--';
                    $jso['UserInfo'][$i]['KindName'] = '--';
                    $jso['UserInfo'][$i]['GameID'] = '--';
                    $jso['UserInfo'][$i]['ServerName'] = '--';
                    $jso['UserInfo'][$i]['AccountsType'] = '3';
                }
            }

            return $this->fetch('', [
                'CHANNELS' => $CHANNELS,
                'u' => $jso['UserInfo'] ?? [],
            ]);
        }

    }


    /**
     *  @brief  : 用户管理-房卡消耗统计
     *  @return : page
     *  @details:
     */
    public function trendAction(){
        $conds = [];
        $order = [];
        $COST_TYPE = config('COST_TYPE');
        $CHANNELS = $this->GetAdminChannel();
        $GAME_KIND = GetGameKind();
        ////////////////////////////////////////////////////////////////////////////
        $Channel = $this->request->param('Channel/d', -1);
        if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
            $conds['d.Channel'][] = ['IN', array_keys($CHANNELS)];
            $conds['d.Channel'][] = ['EQ', $Channel];
        } else {
            $conds['d.Channel'] = ['IN', array_keys($CHANNELS)];
        }
        //////////////////////////////////////////
        $KindID = $this->request->param('KindID/d', -1);
        if ($KindID > 0 && array_key_exists($KindID, $GAME_KIND)) {
            $conds['e.KindID'] = $KindID;
        }
        //////////////////////////////////////////
        $time1 = $this->request->param('time1/s', '', 'trim,YmdHis2Unix');
        $time2 = $this->request->param('time2/s', '', 'trim,YmdHis2Unix');
        $time2 = ($time2 > 0 && $time2 <= time()) ? $time2 : time();
        $time1 = ($time1 > 0 && $time1 <= $time2) ? $time1 : time() - 15 * 24 * 60 * 60; // 默认半个月的数据
        $time1 = date('Y-m-d 00:00:00', $time1);
        $time2 = date('Y-m-d 23:59:59', $time2);
        $conds['a.InsertTime'] = ['BETWEEN TIME', [$time1, $time2]];
        ////////////////////////////////////////////////////////////////////////////
        $list = [];
        $sql2 = table('RecordDrawScore a', 'rydb.record')->field([
            'CONVERT(VARCHAR(10), [a].[InsertTime], 120)' => 'y', 'COUNT(*)' => 'c',
        ])
            ->join('CYBasicDB.dbo.AccountsInfo d', 'd.UserID = a.UserID', 'LEFT')
            ->join('CYRecordDB.dbo.RecordDrawInfo e', 'e.DrawID = a.DrawID', 'LEFT')
            ->group('CONVERT(VARCHAR(10), [a].[InsertTime], 120)')
            ->where($conds)->buildSql();

        $total = db('', 'rydb.record')->table("{$sql2} b")->count();  // 按日期分组满足条件有几条
        if($total>0) {
            $list = table('RecordDrawScore a', 'rydb.record')->field([
                'CONVERT(VARCHAR(10), [a].[InsertTime], 120)' => 'y',
                'DATEPART(hh, [a].[InsertTime])' => 'h',
                'SUM([a].[Score])' => 'c',
            ])
                ->join('CYBasicDB.dbo.AccountsInfo d', 'd.UserID = a.UserID', 'LEFT')
                ->join('CYRecordDB.dbo.RecordDrawInfo e', 'e.DrawID = a.DrawID', 'LEFT')
                ->group('CONVERT(VARCHAR(10), [a].[InsertTime], 120), DATEPART(hh, [a].[InsertTime])')
                ->where($conds)->order(['y' => 'DESC', 'h'])->select();
        }

        $path = Request::instance()->baseUrl();
        $page = new \org\dtcms($list, $total <= 0 ? 1 : $total, 1, $total, false, ['path' => $path]);
        ////////////////////////////////////////////////////////////////////////////
        $tree = [];
        $totalALL = 0;
        foreach($list as $a) {
            $y = $a['y'];
            if (!isset($tree[$y])) {
                $tree[$y] = ['total' => 0, 'child' => []];
            }
            $tree[$y]['total'] += $a['c'];
            $tree[$y]['child'][$a['h']] = $a;
            $totalALL += $a['c'];
        }
        ////////////////////////////////////////////////////////////////////////////
        return $this->fetch('', [
            'tree'      => $tree,
            'page'      => $page,
            'time1'     => $time1,
            'time2'     => $time2,
            'Channel'   => $Channel,
            'info'      => [
                'AvgDay'    => (count($tree)<=0 ? 0 : $totalALL/count($tree)),
                'TotalDay'  => count($tree),
                'TotalALL'  => $totalALL,
            ],
            'COST_TYPE' => $COST_TYPE,
            'CHANNELS'  => $CHANNELS,
            'GAME_KIND' => $GAME_KIND,
            'KindID'    => $KindID,
        ]);
    }

    /**
     *  @brief  : 金币私人场
     *  @return : page
     *  @details:
     */
    public function privateAction(){
        $conds = [];
        $order = [];
        $CHANNELS   = $this->GetAdminChannel();
        $GAME_KIND  = GetGameKind();
        $Channel = $this->request->param('Channel/d', -1);
        if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
            $conds['c.Channel'][] = ['IN', array_keys($CHANNELS)];
            $conds['c.Channel'][] = ['EQ', $Channel];
        } else {
            $conds['c.Channel'] = ['IN', array_keys($CHANNELS)];
        }
        //////////////////////////////
        $KindID = $this->request->param('KindID/d', -1);
        //var_dump($KindID);
        if ($KindID > 0 && array_key_exists($KindID, $GAME_KIND)) {
            $conds['a.KindID'] = $KindID;
        }

        ////////////////////////////////////////////////////////////////////////////
        $UserID = $this->request->param('UserID/d', 0); // 用户号
        if ($UserID > 0) {
            $conds['b.AUID|a.FamilyID'] =$UserID;
        }
        ////////////////////////////////////////////////////////////////////////////
        $orderMap = [
            'A' => 'FamilyID',
            'C' => 'KindID',
            'D' => 'ServerID',
            'E'  => 'UserID',
            'F' => 'GameID',

        ];
        $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'A';
        $order[$orderMap[$this->orderField]] = $this->orderDirection;

        $list = table('FamilyGoldRoom a', 'rydb.websms')->field([
            'a.FamilyID','a.KindID','a.ServerID','f.ServerName','b.AUID '=>'UserID',
            'c.GameID', 'c.Accounts', 'c.NickName', 'c.AccountsType', 'c.LastLogonIP',
            'c.RegisterIP', 'c.GameLogonCount', 'c.LastLogonDate', 'c.RegisterDate',
            'c.RegisterMobile', 'c.Compellation', 'c.PassPortID', 'c.AccountImgUrl',
            'c.Sex', 'c.Channel', 'e.GameName'=>'KindName'
        ])
            ->join('Family b','b.Code = a.FamilyID','LEFT')
            ->join('CYBasicDB.dbo.AccountsInfo c','c.UserID = b.AUID','LEFT')
            ->join('CYBasicDB.dbo.GameKindItem e','e.KindID = a.KindID','LEFT')
            ->join('CYBasicDB.dbo.GameRoomInfo f','f.ServerID = a.ServerID','LEFT')
            ->where($conds)->order($order)->paginate();

        ////////////////////////////////////////////////////////////////////////////
        return $this->fetch('', [
            'list'  => $list,
            'UserID'=> empty($UserID) ? '' : $UserID,
            'CHANNELS' => $CHANNELS,
            'GAME_KIND' => $GAME_KIND,
            'Channel'   => $Channel,
            'KindID'    => $KindID,

        ]);
    }
}